<{include file="public/header" /}>
<body>
<!-- 顶部搜索 -->
<div class="layui-form">
  <div class="layui-form-item" style="margin-top: 10px;">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-inline" >
      <input type="text" name="truename" autocomplete="off" id="truename" placeholder="学生姓名" class="layui-input">
    </div>
    <label class="layui-form-label" style="width: 30px;">班级</label> 
      <div class="layui-input-inline">
        <input type="text" name="class" autocomplete="off"  id="class" placeholder="数字" class="layui-input" lay-verify="number">
      </div>
    <button class="layui-btn" data-type="reload" id="search"><i class="layui-icon layui-icon-search"></i>搜索</button>
    <button class="layui-btn" data-type="reload" id="refresh"><i class="layui-icon layui-icon-refresh"></i>刷新</button>
  </div>
</div>
<!-- 顶部搜索end -->
<table class="layui-hide" id="tlist" lay-filter="tlist" ></table>
 <script id="everyscore" type="text/html">
  <ul>
  {{# var l=1; }}
  {{#  layui.each(d.score, function(i, item){ }}
      <span>{{ l }}题：{{ item }}分,{{# l++;}}</span>
      <!-- <span>{{ item.alias }}：</span>
      <span>{{ item.site || '' }}</span> -->
  {{#  }); }}
  {{#  if(d.score === null){ }}
    白卷
  {{#  } }} 
  </ul>
</script>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <!-- <button class="layui-btn layui-btn-sm" lay-event="addData"><i class="layui-icon layui-icon-add-circle"></i>添加学生</button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="getCheckData"><i class="layui-icon layui-icon-delete"></i>批量删除</button> -->
    <!-- <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
    <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button> -->
    <div class="layui-text layui-inline" style="line-height: 30px;margin: 0 10px 10px 10px;">学生成绩表，右侧按钮可导出下载</div>
  </div>
</script>
 
<script type="text/html" id="barDemo">
  <!-- <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="banli">办理</a> -->
  <!-- <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a> -->
</script>      
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 --> 
 <div style="width: 100%;height: 180px;"></div>
<script>
layui.use(['form','laypage','table','element','laydate','jquery'], function(){
  var table = layui.table,laypage=layui.laypage,element=layui.element,$ = layui.$;
  var date=new Date();

    $('#refresh').on('click',function(){
    	window.location.reload()
    })
  //数据表格配置
  table.render({
    elem: '#tlist'
    ,url:'<{:url('showstuanswers_data')}>'+'?eid='+'<{$eid}>'
    ,toolbar: '#toolbarDemo'
    ,defaultToolbar: ['filter', 'print', 'exports']
    ,title: '<{$title}>'+'_成绩表_siku在线考试系统'+date
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'id', title:'ID', sort: true,width:80}
      ,{field:'getstudent', title:'姓名',templet:function(d){return d.getstudent.truename}}
      ,{field:'getstudent.snumber', title:'学号',templet:function(d){return d.getstudent.snumber}}
      // ,{field:'getclass', title:'班级名称',templet:function(d){return d.getclass.title}}
      ,{field:'getstudent', title:'班级编号', sort: true,templet:function(d){return d.getstudent.class}}
      ,{field:'getstudent', title:'年级',sort: true,templet:function(d){return check_grade(d.getstudent.grade)}}
      ,{field:'score', title:'明细', sort: true,width:200,templet:'#everyscore'}
      ,{field:'total', title:'总分', sort: true}
      ,{field:'c_time', title:'提交时间',width:160,sort: true}
      //,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:160}
    ]]
    ,page: true
    ,id: 'hereReload1'
  });
  
  //头工具栏事件
  table.on('toolbar(tlist)', function(obj){
    //console.log(obj);
    var checkStatus = table.checkStatus(obj.config.id);
    switch(obj.event){
      case 'getCheckData':
        var data = checkStatus.data;
        //layer.alert(JSON.stringify(data));
        //console(data.field);
        var id_str='';
		if (data.length > 0) {
			for (var i = 0; i < data.length; i++) {
			id_str += data[i].sid + ',';
			}
			id_str = id_str.substr(0, id_str.length-1);//获取选中id
			
			alert(id_str);
			//异步批量删除选中的数据
			layer.confirm('确定删除所选学生?', {btn: ['确定','取消'],icon: 3, title:'提示'}
	        ,function(index){
	          //do something
	            $.post("<{:url('delcheck')}>",{id:id_str},function(data,status){
	              //alert(data);
	              //alert(JSON.stringify(data));
	              //console(data);
	              if(data===1){
	                //layer.close(index);
	                layer.msg('删除成功！',{time:1000},function(){
	                  window.location.reload()
	                }); 
	              }else{
	              	
	                layer.alert('删除数据错误，请刷新重试！',{icon:5},function(index){layer.close(index);});
	              }
	           })
	          
	        });
	    }else{
	    	layer.msg('没有选中数据！')
	    }
       //  ,function(){
       //    //点击取消的返回值
       //    //layer.msg('quxiao')
       //    window.location.reload()
       // });
		//异步批量删除选中的数据end
      break;
      case 'addData':
      	layer.open({
      	  title:'添加学生'
		  ,type: 2, 
		  offset: '10px'
		  ,area: ['600px', '480px']
		  ,content: '<{:url('add')}> '//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		}); 
      break;
      // case 'getCheckLength':
      //   var data = checkStatus.data;
      //   layer.msg('选中了：'+ data.length + ' 个');
      // break;
      // case 'isAll':
      //   layer.msg(checkStatus.isAll ? '全选': '未全选');
      // break;
    };
  });
  
  //监听行工具事件
  table.on('tool(tlist)', function(obj){
    var data = obj.data;
    //console.log(obj);
    //alert(data.sid);
    if(obj.event === 'del'){
      //alert(data.sid);
      layer.confirm('真的删除【'+data.truename+'】的数据吗？', function(index){
         $.post("<{:url('del')}>",{sid:data.sid},function(data,status){
	          //alert(data);
	          //alert(JSON.stringify(data));
	          //console(data);
	          if(data===1){
	            layer.close(index);
	            obj.del();
	            layer.msg('删除成功！',{time:1000})
	          }else{
	          	layer.close(index);
	            layer.alert('删除数据错误，请刷新重试！',{icon:5});
	          }
	     })
      });
    } else if(obj.event === 'edit'){
      var data=obj.data;
      var str='<{:url("edit",["id"=>"tid"])}>';
      var url=str.replace("tid",data.sid);
      //alert(url);
      layer.open({
      	  title:'修改信息'
		  ,type: 2 
		  //,offset: '10px'
		  ,area: ['600px', '470px']
		  ,content: url
      })
    }else if (obj.event ==='banli') {
        	var data=obj.data;
          if (data.status==1) {
            layer.confirm('确定标记为&nbsp;<b style="color:#FF5722;">未办理</b>&nbsp;吗？', {btn: ['确定','取消'],icon: 3, title:'提示'}
              ,function(index){
                //do something
                  $.post("<{:url('change_status')}>",{id:data.id,status:2},function(data,status){
                    //alert(data);
                    //alert(JSON.stringify(data));
                    //console(data);
                    if(data===1){
                      layer.close(index);
                      layer.msg('已标记为未办理！',{time:1000},function(){
                        obj.update({
                          status: 2
                        });
                      }); 
                    }else{
                      layer.close(index);
                      layer.alert('标记失败，请刷新重试！',{icon:5});
                    }
                 })
                
              });
          } else {
              layer.confirm('确定标记为&nbsp;<b style="color: #009688;">已办理</b>&nbsp;吗？', {btn: ['确定','取消'],icon: 3, title:'提示'}
              ,function(index){
                //do something
                  $.post("<{:url('change_status')}>",{id:data.id,status:1},function(data,status){
                    //alert(data);
                    //alert(JSON.stringify(data));
                    //console(data);
                    if(data===1){
                      layer.close(index);
                      layer.msg('已标记为已办理！',{time:1000},function(){
                        obj.update({
                          status: 1
                        });
                      }); 
                    }else{
                      layer.close(index);
                      layer.alert('标记失败，请刷新重试！',{icon:5});
                    }
                 })
                
              });
          }
          //console.log(obj.tr);
        };
  });
  //表格重载
	  var active = {
	    reload: function(){
	      var truename = $('#truename'),classnum = $('#class');
	      
	      //执行重载
	      table.reload('hereReload1', {
	        page: {
	          curr: 1 //重新从第 1 页开始
	        }
	        ,where: {
	          //key: {
	            truename: truename.val(),class:classnum.val()
	          //}
	        }
	      });
	    }
	  };
	  
	  $('#search').on('click', function(){
	    var type = $(this).data('type');
	    active[type] ? active[type].call(this) : '';
	  });
})
</script>
<div style="height: 150px;background-color: white;"></div>
</body>
</html>
